package com.mall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description 定义线程池
 * @ClassName MyThreadPoolConfiguration
 * @Author cxx
 * @Date 2023/5/13 14:38
 * @Version 1.0
 */
@Configuration
public class MyThreadPoolConfiguration {

    @Bean(name = "myThreadPool")
    public Executor myThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//设置常驻线程
        executor.setMaxPoolSize(20);//设置最大线程
        executor.setKeepAliveSeconds(60);//设置除常驻线程外的线程存活时间
        executor.setQueueCapacity(30);//设置工作队列大小
        executor.setThreadNamePrefix("my-thread-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());//设置拒绝策略
        executor.initialize();
        return executor; // 返回自身实例作为Bean
    }


}
